<?php

class Default_FavoritesController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Session::start();
        $Session = new Zend_Session_Namespace ('Params');
        unset ($Session->Type);
        unset ($Session->Search);
        unset ($Session->Saved);
        $this->view->Persisted   = $Session->Persisted; 
     #   unset ($Session->Persisted);
        /* Initialize action controller here */
    }

    public function listAction()
    {
        $request    = $this->getRequest();  
        $pagenumber = $request->getParam('page'); 
        $findparam  = $request->getParam('param'); 
        $p = new Default_Model_UserProfile; 
        $p->GetBookmarks($findparam); 

        $startIndex = ($pagenumber - 1) * 8;   
        $this->view->bookmarks  = array_slice ($p->bookmarks, $startIndex, 8);
        $this->view->size       = sizeof($p->bookmarks);
        $this->view->page       = $pagenumber;
        $this->view->param      = $findparam;
    }

    public function browseAction()
    {
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('group'); 
        $pagenumber = $request->getParam('page'); 
        $key        = $request->getParam('key'); 
        $item       = $request->getParam('item'); 
        $show       = $request->getParam('show');  

        $b = new Default_Model_BookmarkManager;
        $bookmark = $b->Find ($groupname, $key);
        $DATA_URI  = DATA_PATH . "/bookmark/{$bookmark->Path}";
        $bookmark->Populate ($DATA_URI);


        $targetURL  = "/favorites/load/page/{$pagenumber}/key/{$key}/group/{$groupname}/show/";

           $startIndex = ($show - 1) * 8;  
            $this->view->articles = array_slice ($bookmark->Items, $startIndex, 8);

        $this->view->group       = $groupname;
        $this->view->page        = $pagenumber;
        $this->view->key         = $key;
        $this->view->prefix      = $targetURL; 
        $this->view->articlepage = $show; 
        $this->view->articlesize = sizeof($bookmark->Items); 
        $this->view->subject     = $bookmark->Name;

    }

    public function loadAction()
    {
        $request    = $this->getRequest(); 
        $key        = $request->getParam('key'); 
        $item       = $request->getParam('item'); 
        $groupname  = $request->getParam('group');  
        $pagenumber = $request->getParam('page'); 
        $show       = $request->getParam('show');  
        if (!isset($item)) $item = $key;
        $b = new Default_Model_BookmarkManager;
        $bookmark = $b->Find ($groupname, $key);

        if (!$bookmark) {
            echo "Bookmark $groupname, $key not found!";
            return;
        }

        $DATA_URI  = DATA_PATH . "/bookmark/{$bookmark->Path}";

 

        $bookmark->Populate ($DATA_URI);
      
        $targetURL  = "/favorites/view/page/{$pagenumber}/key/{$key}/group/{$groupname}/item/{$item}";

        $message = $this->_helper->getHelper("FlashMessenger")->getMessages(); 
        if($message){
            $this->_helper->layout->setLayout('ios');
            $this->view->links = array(array('text'=>'Loading...')); 
            $targetURL = $message[0];  
        }

        $data = array ('groupname'  => $groupname,
                        'endpoint'   => ARTICLE_CLIENT, 
                        'article'    => $item);

        if (isset($show)) {
 
            $startIndex = ($show - 1) * 8;  
            $picsonpage = array_slice ($bookmark->Items, $startIndex, 8);
            $keys = array();
            foreach ($picsonpage as $pic) $keys[] = $pic->id;

            $data = array ('groupname'  => $groupname,
                            'endpoint'   => ARTICLE_CLIENT, 
                            'article'    => implode('/', $keys) );
            $targetURL  = "/favorites/browse/page/{$pagenumber}/key/{$key}/group/{$groupname}/item/{$item}/show/{$show}";
        } 


        $MSGKEY = 519059;  # TO DO: Config 
        $Queue  = new  Default_Model_QueueManager($MSGKEY, $data); 
        $Id     = $Queue->SendMessage($show=='show' ? NULL : $targetURL);  

        $this->view->Key = $Id; 

        if (false) {
            $this->_helper->layout->disableLayout();
            echo $Id;
            return;
        } 
    }

    public function viewAction()
    {
        $request    = $this->getRequest(); 
        $key        = $request->getParam('key'); 
        $item       = $request->getParam('item'); 
        $pagenumber = $request->getParam('page');  
        $groupname  = $request->getParam('group');  

        $this->_helper->layout->setLayout('blank');
        $b = new Default_Model_BookmarkManager;
        $bookmark = $b->Find ($groupname, $key);

        $DATA_URI  = DATA_PATH . "/bookmark/{$bookmark->Path}";
        $bookmark->Populate ($DATA_URI);
        $this->view->page = $pagenumber;
        $this->view->subject = $bookmark->Name;
        $this->view->items   = $bookmark->Items;
        $this->view->group   = $bookmark->Group;
        $this->view->key     = $key;
        $this->view->item    = $item;
    }

    public function indexAction()
    {
        $request    = $this->getRequest();  
        $pagenumber = $request->getParam('page'); 
        $p = new Default_Model_UserProfile; 
        $p->GetBookmarks(); 

        $startIndex = ($pagenumber - 1) * 8;   
        $bookmarks  = array_slice ($p->bookmarks, $startIndex, 8);


        $key = array();
        foreach ($bookmarks as $pic) $key[] = $pic->Group . ":" . $pic->Id;
 
            $data = array ('groupname'  => $groupname,
                            'endpoint'   => ARTICLE_CLIENT, 
                            'article'    => implode('/', $key) );
            $targetURL  = "/favorites/list/page/{$pagenumber}";


        $MSGKEY = 519059;  # TO DO: Config 
        $Queue  = new  Default_Model_QueueManager($MSGKEY, $data); 
        $Id     = $Queue->SendMessage($targetURL);   


    }

    public function bookmarkAction()
    {
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('group'); 
        $index      = $request->getParam('key'); 
        $page       = $request->getParam('page'); 
        $origin     = $request->getHeader('referer'); 
        $m = new Default_Model_BookmarkManager;
        $m->RemoveByPath($groupname, $index) ;
        echo "<script>location.href='{$origin}';</script>"; 
    }

    public function homeAction()
    {
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('group'); 
        $pagenumber = $request->getParam('page');  
        if (!isset($pagenumber)) $pagenumber = 1;
        $startIndex = ($pagenumber - 1) * 10;


       // $this->_helper->layout->setLayout('blank');
        $m = new Default_Model_BookmarkManager;
        if (!isset($groupname)) $groupname = $m->Bookmarks[0]->Group;
        $m->Collate();

        $this->view->size      = sizeof($m->Collated[$groupname]);
        $this->view->page      = $pagenumber;

        $this->view->items     = array_slice ($m->Collated[$groupname], $startIndex, 10);

        $this->view->bookmarks = $m->Collated;
        $this->view->groupname = $groupname;
    }

    public function sortAction()
    {
        $request    = $this->getRequest(); 
        $groupname  = $request->getParam('group');  
        $m = new Default_Model_BookmarkManager;
        $m->Collate();
        $bookmarks = $m->Collated; 
        $this->view->groupname = $groupname;
        if (is_array($bookmarks[$groupname])) {


            $key = array();
            foreach ($bookmarks[$groupname] as $pic) $key[] = $pic->Id;

            $data = array ('groupname'  => $groupname,
                            'endpoint'   => ARTICLE_CLIENT, 
                            'article'    => implode('/', $key) );
            $targetURL  = "/favorites/home/group/{$groupname}"; 

 
            $MSGKEY = 519059;  # TO DO: Config 
            $Queue  = new  Default_Model_QueueManager($MSGKEY, $data); 
            $Id     = $Queue->SendMessage($targetURL);  
        
        }
 
    }


}







